package com.qf.dbUtil.common.aspect;

import com.github.pagehelper.util.StringUtil;
import com.qf.dbUtil.common.exception.AppException;
import com.qf.dbUtil.common.result.ResponseEnum;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 功能说明
 *
 * @author yihuan
 * @data 2020-10-14  11:35:28
 */
@Component
@Aspect
public class AuthAspect {
    //ProceedingJoinPoint 可执行切入点  是JoinPoint的子类
    //环绕
    @Around("@annotation(com.qf.dbUtil.common.annotation.CheckLogin)")
    public Object checkLogin(ProceedingJoinPoint pjp) throws Throwable {
        //1:获取request RequestContextHolder上下文容器（springmvc）
        ServletRequestAttributes requestAttributes =
        (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String userstr = request.getParameter("userstr");
        if(StringUtil.isEmpty(userstr)){
            throw new AppException(ResponseEnum.NO_LOGIN);
        }
        Object proceed = pjp.proceed();
        return proceed;
    }
}
